#include "DeductionView.h"

#include <boost/bind.hpp>
#include "../../models/network/CardViewModel.h"
#include "../../helpers/graphic/ColoursPalette.h"
#include "../../helpers/remote/deduction/ShowDeductedCardsDialog.h"

using namespace Views::Remote;
using namespace Views::Helpers::Graphic::Deduction;
using namespace Utils;

DeductionView::DeductionView(Utils::GUI::Form* form, Domain::Network::GameClient* client, Cards::CardsCollection cards):
Graphic::DeductionView(form), client(client), cards(cards), CardInfo(NULL)
{
}

void DeductionView::openPickSearchedCardsDialog(Players::Player* player, Cards::PlayersCards players_cards,
	Cards::WeaponsCards weapons_cards)
{
	client->doOnReceiveData(Domain::Configuration::Network::DEDUCTION_SEARCHED_CARDS,
		boost::bind(&DeductionView::onSearchedCardsPick, this, _1));
}

void DeductionView::onSearchedCardsPick(sf::Packet packet)
{
	Views::Models::Network::CardViewModel cvm(cards);
	Cards::PlayerCard* player_card = NULL;
	packet >> cvm;
	player_card = dynamic_cast<Cards::PlayerCard*>(cvm.card);
	Cards::WeaponCard* weapon_card = NULL;
	packet >> cvm;
	weapon_card = dynamic_cast<Cards::WeaponCard*>(cvm.card);
	searchedCardsPickEvent(player_card, weapon_card);
}

void DeductionView::openPickCardDialog(Players::Player* player, Cards::CardSet& common_cards)
{
	if (player == client->getPlayerClient()->getPlayer())
	{
		Graphic::DeductionView::openPickCardDialog(player, common_cards);
	}
	else
	{
		client->doOnReceiveData(Domain::Configuration::Network::DEDUCTION_PICK_CARD,
			boost::bind<void, DeductionView, sf::Packet>(&DeductionView::onDeductedCardPick, this, _1));
	}
}

void DeductionView::onDeductedCardPick(sf::Packet packet)
{
	Views::Models::Network::CardViewModel cvm(cards);
	packet >> cvm;
	deductedCardPickEvent(cvm.card);
}

void DeductionView::onDeductedCardPick(Cards::Card* card)
{
	sf::Packet packet;
	Views::Models::Network::CardViewModel cvm(card);
	packet << cvm;
	client->send(Domain::Configuration::Network::DEDUCTION_PICK_CARD, packet);

	Graphic::DeductionView::onDeductedCardPick(card);
}

void DeductionView::showDeductedCard(Cards::Card* card, Players::Player* current_player, Players::Player* card_owner)
{
	CardInfo = new Helpers::Remote::Deduction::ShowDeductedCardsDialog(current_player, card_owner);
	Form->addChild(CardInfo);
	CardInfo->MoveCenter();
	Form->Draw(true);

	client->doOnReceiveData(Domain::Configuration::Network::DEDUCTION_CARD_SHOW,
		boost::bind(&DeductionView::onDeductedCardsDialogClose, this, _1));
}

void DeductionView::showNoCardsDeducted(Cards::CardSet& cards, Players::Player* player)
{
	CardInfo = new Helpers::Remote::Deduction::ShowNoCardsDeducted();
	Form->addChild(CardInfo);
	CardInfo->MoveCenter();
	Form->Draw(true);

	client->doOnReceiveData(Domain::Configuration::Network::DEDUCTION_CARD_SHOW,
		boost::bind(&DeductionView::onDeductedCardsDialogClose, this, _1));
}

void DeductionView::onDeductedCardsDialogClose(sf::Packet packet)
{
#ifdef _DEBUG
	std::clog << __FUNCTION__ << std::endl;
#endif
	delete CardInfo;
	CardInfo = NULL;
	deductedCardsShowEvent();
}

